package com.tian.service;

import com.tian.dto.LoanFeeReqDto;
import com.tian.dto.LoanFeeRespDto;
import com.tian.dto.UserChargeRecordQueryReqDto;
import com.tian.dto.UserChargeRecordQueryRespDto;
import com.tian.entity.UserChargeRecord;
import com.tian.entity.UserTradeRecord;
import com.tian.mapper.UserChargeRecordMapper;
import com.tian.mapper.UserTradeRecordMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年10月10日 21:15
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 充值记录
 */
@Service(version = "1.0.0")
public class UserChargeRecordServiceImpl implements UserChargeRecordService {

    @Resource
    private UserTradeRecordMapper userTradeRecordMapper;
    @Resource
    private UserChargeRecordMapper userChargeRecordMapper;
    @Resource
    private LoanFeeService loanFeeService;

    @Override
    public void add() {

    }

    @Override
    public List<UserChargeRecordQueryRespDto> query(UserChargeRecordQueryReqDto userChargeRecordQueryReqDto) {

        List<UserChargeRecordQueryRespDto> userChargeRecordQueryRespDtoList = new ArrayList<>();

        String customerNo = userChargeRecordQueryReqDto.getCustomerNo();
        int start = userChargeRecordQueryReqDto.getStart();
        int pageSize = userChargeRecordQueryReqDto.getPageSize();

        //通过客户号，按照分页形式 查询该客户所有充值交易记录
        List<UserTradeRecord> userTradeRecordList = userTradeRecordMapper.selectByCustomerNo(customerNo, 1, start, pageSize);

        List<String> tradeNos = new ArrayList<>();
        //遍历所有充值交易记录
        for (UserTradeRecord userTradeRecord : userTradeRecordList) {
            tradeNos.add(userTradeRecord.getTradeNo());
        }

        List<UserChargeRecord> userChargeRecords = userChargeRecordMapper.selectByTradeNos(tradeNos);

        for (UserChargeRecord userChargeRecord : userChargeRecords) {
            UserChargeRecordQueryRespDto userChargeRecordQueryRespDto = new UserChargeRecordQueryRespDto();

            userChargeRecordQueryRespDto.setAmount(userChargeRecord.getAmount());
            userChargeRecordQueryRespDto.setCreateTime(userChargeRecord.getCreateTime());

            LoanFeeReqDto loanFeeReqDto = new LoanFeeReqDto();
            loanFeeReqDto.setAmount(userChargeRecord.getAmount());
            loanFeeReqDto.setPayType(userChargeRecord.getPayType());
            loanFeeReqDto.setCustNo(customerNo);
            LoanFeeRespDto loanFeeRespDto = loanFeeService.calculate(loanFeeReqDto);

            //实际到账金额 = 充值金额 - 总费用
            userChargeRecordQueryRespDto.setActualReceivedAmount(userChargeRecord.getAmount().subtract(loanFeeRespDto.getTotalFee()));

            userChargeRecordQueryRespDto.setTradeNo(userChargeRecord.getTradeNo());
            userChargeRecordQueryRespDto.setFeeAmount(loanFeeRespDto.getTotalFee());
            userChargeRecordQueryRespDto.setPayType(userChargeRecord.getPayType());

            //添加到列表中
            userChargeRecordQueryRespDtoList.add(userChargeRecordQueryRespDto);
        }

        //返回该customerNo客户的充值记录列表
        return userChargeRecordQueryRespDtoList;
    }
}
